﻿Imports System.IO
Imports System.ServiceProcess

Public Class Service1
    Public fs As FileStream
    Public fw As StreamWriter


    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.
        Timer1.Enabled = True
        Timer1.Interval = My.Settings.killInterval
        If (Not File.Exists(My.Settings.logFile)) Then
            File.Create(My.Settings.logFile)
        End If
    End Sub

    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.
        Timer1.Enabled = False
        
    End Sub

    Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
        Try

            fw = New StreamWriter(My.Settings.logFile, True)
            fw.WriteLine("{0} Starte..", Now.ToString())

            Dim sc() As ServiceController = ServiceController.GetServices()
            For Each serviceToStop As String In My.Settings.servicesToStop
                For Each service As ServiceController In sc
                    If (serviceToStop.ToLower().Trim() = service.ServiceName.ToLower().Trim()) Then
                        If (service.Status = ServiceControllerStatus.Running) Then
                            service.Stop()
                            fw.WriteLine("{0} Stoppe Service {1}", Now.ToString(), service.ServiceName)
                        End If
                    End If
                Next
            Next

            fw.WriteLine("{0} Beende...", Now.ToString())
            fw.Close()

        Catch ex As Exception
            fw.WriteLine("Fehler: " & ex.Message.ToString())
        End Try

    End Sub
End Class
